// websocket初始化建立连接
// import store from '../store'
import { getHost } from '../utils/auth'
export function getWsHost () {
  let href = window.location.href
  let host = href.split('//')[1].split('/')[0]
  let wsHead = 'ws'
  let proxyStr = ''
  let apiPrefix = getHost() ? getHost() : 'openatc'
  if (href.indexOf('https') > -1) {
    wsHead = 'wss'
  }
  if (process.env.NODE_ENV === 'development') {
    proxyStr = '/socket'
    host = location.host
    apiPrefix = 'openatc'
    // return 'ws://192.168.13.103:10003/openatc/ws'
  }
  let res = `${wsHead}://${host}${proxyStr}/${apiPrefix}/ws`
  console.log('ws_url', res)
  return res
}

export function InitWSSubMgr (store) {
  return new Promise((resolve, reject) => {
    store.dispatch('InitWsSubMgrObj')
    let ssSubMgr = store.getters['wsSubMgr']
    let hostapi = getWsHost()
    ssSubMgr.createnew('CrossState', hostapi, store).then(() => {
      resolve()
    })
  })
}
